<?php

namespace app\store\controller;

use app\store\model\BillingInfoModel;
use app\store\model\TradeAmountModel;
use app\store\model\TradeCannonModel;
use app\store\model\TradeLevelModel;
use library\Controller;
use think\Db;
use \app\store\model\TradeModel;

/**
 * 充值数据
 * Class task
 * @package app\store\controller
 */
class Trade extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    public static $CHANNEL = 888;//平台渠道
    protected $table_DB_Platform_Account_log_library = 'db_platform_log';       /*平台日志库*/
    protected $table_DB_Platform_Account_library = 'db_platform_accounts';      /*平台账户配置库*/
    protected $table_DB_Platform_Account_statistics = 'db_platform_statistics'; /*平台账户配置库*/
    protected $day_user_online = 'day_user_online';                             /* 在线用户统计 */
    protected $accounts_info = 'accounts_info';                                 /* 用户表 */
    protected $user_tradelog = 'tradelog';                                      /* 订单记录表 */
    protected $user_stat_remain = 'stat_remain';                                      /* 留存记录表 */
    protected $user_recharge_info = 'recharge_info';                            /* 充值情况记录表 */
    protected $user_data_info = 'data_info';                                    /* 日活记录表 */
    protected function Db_connect_day_user_online()
    {
        $day_user_online = Db::connect($this->table_DB_Platform_Account_log_library)->table($this->day_user_online);
        return $day_user_online;
    }

    protected function Db_connect_accounts_info()
    {
        $accounts_info = Db::connect($this->table_DB_Platform_Account_library)->table($this->accounts_info);
        return $accounts_info;
    }

    protected function Db_connect_user_data_info()
    {
        $user_data_info = Db::connect($this->table_DB_Platform_Account_statistics)->table($this->user_data_info);
        return $user_data_info;
    }

    protected function Db_connect_user_recharge_info()
    {
        $user_recharge_info = Db::connect($this->table_DB_Platform_Account_statistics)->table($this->user_recharge_info);
        return $user_recharge_info;
    }

    protected function Db_connect_user_tradelog()
    {
        $user_tradelog = Db::connect($this->table_DB_Platform_Account_library)->table($this->user_tradelog);
        return $user_tradelog;
    }

    protected function Db_connect_user_stat_remain()
    {
        $user_stat_remain = Db::connect($this->table_DB_Platform_Account_library)->table($this->user_stat_remain);
        return $user_stat_remain;
    }

    public function __construct()
    {
        parent::__construct();
        /*渠道类型*/
        $channel_arr =Specific_parameters_Recharge_channel_arr();
        $this->assign('channel_arr',$channel_arr);
    }

    public function day()
    {
        $this->title = '每日充值';
        $request = input('request.');
        $list =$this->Db_connect_user_stat_remain()
            ->where($this->structure_where())
            ->order('stat_time desc')
            ->paginate(30,false,['query'=>$request]);
        $data = $list->all();
        foreach ($data as $k => $v) {
            if($v['channel'] == 888) {$v['channel_name']='全部渠道';}else {$v['channel_name']=get_channel_name($v['channel']);}
            $v['date'] = date('Y-m-d',strtotime($v['stat_time']));
            $list[$k] = $v;
        }
        $data_arr = $list->toarray()['data'];
        $pages = $list->render();
        $start_time =date('Y-m-d',time()-7*24*3600).' - '.date('Y-m-d',time()-24*3600);
        $this->assign('list',$list);
        $this->assign('start_time',$start_time);
        $this->assign('pages', $pages);
        $this->assign('data_arr', $data_arr);
        $this->fetch();
    }

    public function new_add()
    {
        $this->title = '新增付费';
        $request = input('request.');
        $list =$this->Db_connect_user_stat_remain()
            ->where($this->structure_where())
            ->order('stat_time desc')
            ->paginate(30,false,['query'=>$request]);
        $data = $list->all();

        foreach ($data as $k => $v) {
            if($v['channel'] == 888) {$v['channel_name']='全部渠道';}else {$v['channel_name']=get_channel_name($v['channel']);}
            $v['date'] = date('Y-m-d',strtotime($v['stat_time']));
            $list[$k] = $v;
        }
        $pages = $list->render();
        $start_time =date('Y-m-d',time()-7*24*3600).' - '.date('Y-m-d',time()-24*3600);

        $this->assign('list',$list);
        $this->assign('start_time',$start_time);
        $this->assign('pages', $pages);
        $this->fetch();
    }

    public function trade_remain(){
        $this->title = '付费留存';
        $request = input('request.');
        $list =$this->Db_connect_user_stat_remain()
            ->where($this->structure_where())
            ->order('stat_time desc')
            ->paginate(30,false,['query'=>$request]);
        $data = $list->all();

        foreach ($data as $k => $v) {
            if($v['channel'] == 888) {$v['channel_name']='全部渠道';}else {$v['channel_name']=get_channel_name($v['channel']);}
            $v['date'] = date('Y-m-d',strtotime($v['stat_time']));
            $map =[];
            if($v['channel'] != 888) {
                $map['channel_id']=$v['channel'];
            }
            $trade_res = TradeModel::getRechargeNumByDateAndChannel($v['date'],$v['channel'] == 888?null:$v['channel']);
            $v['trade'] = $trade_res;
            $list[$k] = $v;
        }
        $pages = $list->render();
        $start_time =date('Y-m-d',time()-7*24*3600).' - '.date('Y-m-d',time()-24*3600);

        $this->assign('list',$list);
        $this->assign('start_time',$start_time);
        $this->assign('pages', $pages);
        $this->fetch();
    }

    protected function structure_where()
    {
        $channel_type = $this->request->param('channel_type');
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $list = explode(' - ', $search_time);
            $begin = $list[0];
            $after = $list[1];
            $where[] = ['stat_time', 'between', [$begin, $after]];
        } else {
            $begin = date('Y-m-d', time() - 7 * 24 * 3600);
            $after = date('Y-m-d', time() - 24 * 3600);
            $where[] = ['stat_time', 'between', [$begin, $after]];
        }
        if (!empty($channel_type)) {
            if ($channel_type == 30) $channel_type = 0;
            $where[] = ['channel', 'eq', $channel_type];
        } else {
            $where[] = ['channel', 'eq', 888];
        }
        return $where;
    }

    protected function structure_where2()
    {
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $list = explode(' - ', $search_time);
            $begin = $list[0];
            $after = $list[1];
            $where[] = ['time', 'between', [$begin, $after]];
        } else {
            $begin = date('Y-m-d', time() - 7 * 24 * 3600);
            $after = date('Y-m-d', time() - 24 * 3600);
            $where[] = ['time', 'between', [$begin, $after]];
        }

        return $where;
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function distribution(){
        $this->title = '付费分布';

        $list = TradeLevelModel::getTradeLevelByDate($this->structure_where2());
        $list2 = TradeAmountModel::getTradeAmountByDate($this->structure_where2());
        $list3 = TradeCannonModel::getTradeCannonByDate($this->structure_where2());

        $start_time =date('Y-m-d',time()-7*24*3600).' - '.date('Y-m-d',time()-24*3600);
        $amount_rate = [];
        foreach ($list as $k=>$v){
            $amount_rate[$k]['name'] = TradeLevelModel::TRADE_LEVEL[$v['level_dist']];
            $amount_rate[$k]['value'] = $v['level_rate'];
        }

        $amount_rate2 = [];
        foreach ($list2 as $k=>$v){
            $amount_rate2[$k]['name'] = TradeAmountModel::TRADE_AMOUNT[$v['amount_dist']];
            $amount_rate2[$k]['value'] = $v['amount_rate'];
        }

        $amount_rate3 = [];
        foreach ($list3 as $k=>$v){
            $amount_rate3[$k]['name'] = TradeCannonModel::TRADE_CANNON[$v['cannon_dist']];
            $amount_rate3[$k]['value'] = $v['cannon_rate'];
        }

        $this->assign('amount_rate',$amount_rate);
        $this->assign('amount_rate2',$amount_rate2);
        $this->assign('amount_rate3',$amount_rate3);

        $this->assign('trade_level',TradeLevelModel::TRADE_LEVEL);
        $this->assign('trade_amount',TradeAmountModel::TRADE_AMOUNT);
        $this->assign('trade_cannon',TradeCannonModel::TRADE_CANNON);

        $this->assign('list',$list);
        $this->assign('list2',$list2);
        $this->assign('list3',$list3);
        $this->assign('start_time',$start_time);

        $this->fetch();
    }

    /**付费习惯
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function habit(){
        $this->title = '付费习惯';
        $list = BillingInfoModel::where($this->structure_where_billing())
            ->order('time desc')
            ->select();
        foreach ($list as $k => $v) {
            $v['channel_name']=get_channel_name($v['channel_id']);
            $v['billing_name'] = get_Billing_name($v['billingId']);
            // 当天某渠道下所有计费点金额
            $day_channel_total = BillingInfoModel::getRecharge($v['time'],$v['channel_id']);
            // 金额占比
            $v['rate'] = $day_channel_total != 0?(round($v['recharge']/$day_channel_total*100,2)):'--';
            $list[$k] = $v;
        }
        $list = $list->toarray();
        $start_time = date('Y-m-d', time());
        $this->assign('list',$list);
        $this->assign('user_billing_count',$this->user_billing_count());
        $this->assign('user_billing_num',$this->user_billing_num());
        $this->assign('start_time',$start_time);
        $this->fetch();
    }
    /* 计费点充值分布情况 */
    public function user_billing_num(){
        $time=date('Y-m-d',time());
        $where[] =['time','EQ',$time];
        $arr =BillingInfoModel::field('billingId,count')->where($this->structure_where_billing())->select();
        $this->totalJson = ['recharge' => [], 'billing' => []];
        foreach($arr as $value){
            $this->totalJson['count'][] = $value['count'];
            $this->totalJson['billing'][] = get_Billing_name($value['billingId']);
        }
        return $this->totalJson;
    }
    /* 计费点充值分布情况 */
    public function user_billing_count(){
        $time=date('Y-m-d',time());
        $where[] =['time','EQ',$time];
        $arr =BillingInfoModel::field('billingId,recharge')->where($this->structure_where_billing())->select();
        $this->totalJson = ['recharge' => [], 'billing' => []];
        foreach($arr as $value){
            $this->totalJson['recharge'][] = $value['recharge'];
            $this->totalJson['billing'][] = get_Billing_name($value['billingId']);
        }
        return $this->totalJson;
    }
    protected function structure_where_billing()
    {
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $where[] = ['time', 'eq', $search_time];
        } else {
            $begin = date('Y-m-d', time());
            $where[] = ['time', 'eq', $begin];
        }
        return $where;
    }

}
